package com.zadtek.jpsqishi.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zadtek.jpsqishi.R;
import com.zadtek.jpsqishi.activity.common.Common;
import com.zadtek.jpsqishi.activity.common.Const;
import com.zadtek.jpsqishi.activity.data.MytjData;
import com.zadtek.jpsqishi.frame.JsonProcessHelper;

/**
 * 我的统计
 * 
 * @author kuyukeji
 * 
 */
public class MytjChildFragment extends Fragment {

	private Context context;
	private LayoutInflater inflater;
	private Activity myActivity;
	private Resources res;

	private MytjData mytjdata;

	private TextView pinfscore, paimin, totaldan, paiminzd; // 配送服务评分,排名,总单量,单量排名
	private TextView pszsl, paiminps, chaos, chaosd; // 配送准时率,配送准时排名,超时单量
	private ImageView toright;
	
	private LinearLayout scorephlay;	//配送评分排名
	
	private TextView scorename, allname, ratename, c4name, c6name; 

	private SharedPreferences sp;
	private String time = Common.getToday(); // 时间(昨天,今天,本月,上月),默认今天

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	// 设置时间
	public void setTime(String time) {
		this.time = time;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_mytj_child, null);
		context = view.getContext();
		this.inflater = inflater;
		myActivity = getActivity();
		res = getResources();
		
		scorename = (TextView) view.findViewById(R.id.scorename);
		allname = (TextView) view.findViewById(R.id.allname);
		ratename = (TextView) view.findViewById(R.id.ratename);
		c4name = (TextView) view.findViewById(R.id.c4name);
		c6name = (TextView) view.findViewById(R.id.c6name);
		
		pinfscore = (TextView) view.findViewById(R.id.pinfscore);
		paimin = (TextView) view.findViewById(R.id.paimin);
		totaldan = (TextView) view.findViewById(R.id.totaldan);
		paiminzd = (TextView) view.findViewById(R.id.paiminzd);
		pszsl = (TextView) view.findViewById(R.id.pszsl);
		paiminps = (TextView) view.findViewById(R.id.paiminps);
		chaos = (TextView) view.findViewById(R.id.chaos);
		chaosd = (TextView) view.findViewById(R.id.chaosd);
		toright = (ImageView) view.findViewById(R.id.toright);
		scorephlay = (LinearLayout) view.findViewById(R.id.scorephlay);
		scorephlay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent paih = new Intent(getActivity(), PaihFragment.class);
				paih.putExtra("tostate", "1");
				paih.putExtra("totime", time);
				startActivity(paih);
			}
		});

		getinitArray();

		return view;
	}

	// 获取初始化数据
	public void getinitArray() {
		initArrays(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (!"emp".equals(msg.obj.toString())) {
					mytjdata = (MytjData) msg.obj;
					if (mytjdata != null && mytjdata.getStatus().equals("1")) {
						
						pinfscore.setText(mytjdata.getScoretotal());
						paimin.setText(mytjdata.getScoresortorder());
						String scorenote = mytjdata.getScorenote();
						if(scorenote!=null && !scorenote.equals("")) {
							scorename.setText(scorenote);
						}
						totaldan.setText(mytjdata.getAlltotal());
						paiminzd.setText(mytjdata.getAllscortorder());
						allname.setText(mytjdata.getAllnote());
						pszsl.setText(mytjdata.getRatetotal());
						paiminps.setText(mytjdata.getRatesortorder());
						ratename.setText(mytjdata.getRatenote());
						String c4total = mytjdata.getC4total();
						if (c4total != null && !c4total.equals("")
								&& Integer.parseInt(c4total) > 0) {
							chaos.setTextColor(res.getColor(R.color.red));
						} else {
							chaos.setTextColor(res.getColor(R.color.black));
						}
						chaos.setText(c4total);
						c4name.setText(mytjdata.getC4note());
						String c6total = mytjdata.getC6total();
						if (c6total != null && !c6total.equals("")
								&& Integer.parseInt(c6total) > 0) {
							chaosd.setTextColor(res.getColor(R.color.red));
						} else {
							chaosd.setTextColor(res.getColor(R.color.black));
						}
						chaosd.setText(c6total);
						c6name.setText(mytjdata.getC6note());
					} else {
						Common.DisplayToast(context, mytjdata.getFail(), 1);
					}

				} else {
					Common.DisplayToast(context, "通信失败,请检查网络!", 1);
				}
			}
		});
	}

	// 线程加载数据
	private void initArrays(final Handler handler) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					// 从缓存中获取用户信息
					sp = myActivity.getSharedPreferences(Const.PREF_NAME,
							Context.MODE_PRIVATE);
					String uid = sp.getString("uid", "");
					MytjData resultdata = JsonProcessHelper
							.jsonProcess_getoneqstotal("get_one_qishi_total",
									uid, time);
					if (resultdata != null) {
						handler.obtainMessage(0, resultdata).sendToTarget();
					} else {
						handler.obtainMessage(0, "emp").sendToTarget();
					}

				} catch (Exception e) {
					Thread.interrupted();
					e.printStackTrace();
				}
			}
		}).start();
	}

}
